package com.example.uiclassassignments.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import com.example.uiclassassignments.R;
import com.example.uiclassassignments.tools.FragmentViewPager1;
import com.example.uiclassassignments.tools.FragmentViewPager2;
import com.example.uiclassassignments.tools.FragmentViewPager3;

import java.util.ArrayList;

@SuppressLint("Registered")
public class ActionBarActivity extends AppCompatActivity implements ActionBar.TabListener{
    private ArrayList<Fragment> mListFragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_bar);
        mListFragments = new ArrayList<Fragment>();
        mListFragments.add(new FragmentViewPager1());
        mListFragments.add(new FragmentViewPager2());
        mListFragments.add(new FragmentViewPager3());
        initView();

    }
    protected void initView() {
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();

        assert actionBar != null;
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab = actionBar.newTab().setText("Tabl1").setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab().setText("Tabl2").setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab().setText("Tabl3").setTabListener(this);
        actionBar.addTab(tab);

    }

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_content,mListFragments.get(tab.getPosition())).commit();
    }
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
